# !/usr/bin/env python
# -*-coding:utf-8 -*-

"""
# File       : run_number_view.py
# Time       ：2023/10/31 11:49
# Author     ：YangYong
# version    ：python 3.10
# Description：数据可视化
"""
from datetime import datetime, timedelta

from flask_restx import fields

from apis.home import home_ns
from apis.orderlines.models import ProcessInstance
from orderlines.utils.orderlines_enum import ProcessStates
from public.api_utils.api_decorator import handle_api_error
from public.base_model import db
from public.base_response import generate_response, MarshalModel
from public.base_view import AbstractView

run_number = {
    'total_number': fields.Integer(),
    'success_total_number': fields.Integer(),
    'failure_total_number': fields.Integer(),
    'current_success_total_number': fields.Integer(),
    'current_failure_total_number': fields.Integer(),
}
run_number_marshal = MarshalModel('run_number', home_ns)


class RunNumberView(AbstractView):
    url = '/run_number'

    def __init__(self, *args, **kwargs):
        super(RunNumberView, self).__init__(*args, **kwargs)
        self.filter = list()
        self.current_data = f"{datetime.now().strftime('%Y-%m-%d')} 23.59.59"
        self.current_data = f"{datetime.now().strftime('%Y-%m-%d')} 23.59.59"
        self.last_data = f"{(datetime.now() - timedelta(days=1)).strftime('%Y-%m-%d')} 23.59.59"

    def run_status_number(self):
        """获取运行状态数量"""
        total_number = db.session.query(ProcessInstance).filter(
            ProcessInstance.active == 1,
            ProcessInstance.start_time.between(self.last_data, self.current_data)).all()

        success_total_number = db.session.query(ProcessInstance).filter(
            ProcessInstance.active == 1,
            ProcessInstance.process_status == ProcessStates.green.value).all()

        failure_total_number = db.session.query(ProcessInstance).filter(
            ProcessInstance.active == 1,
            ProcessInstance.process_status == ProcessStates.red.value).all()

        current_success_total_number = db.session.query(ProcessInstance).filter(
            ProcessInstance.active == 1,
            ProcessInstance.process_status == ProcessStates.green.value,
            ProcessInstance.start_time.between(self.last_data, self.current_data)).all()

        current_failure_total_number = db.session.query(ProcessInstance).filter(
            ProcessInstance.active == 1,
            ProcessInstance.process_status == ProcessStates.red.value,
            ProcessInstance.start_time.between(self.last_data, self.current_data)).all()
        return {
            'total_number': len(total_number),
            'success_total_number': len(success_total_number),
            'failure_total_number': len(failure_total_number),
            'current_success_total_number': len(current_success_total_number),
            'current_failure_total_number': len(current_failure_total_number)
        }

    @handle_api_error()
    @home_ns.doc(description='运行状态数量')
    @home_ns.marshal_with(run_number_marshal.detail_response(run_number, is_db=False))
    def get(self):
        """运行状态数量"""
        response_data = self.run_status_number()
        return generate_response(response_data)
